import moment from 'moment'
let utils = {

    /**
     * 判断变量是否为空
     */
    isEmpty: function(value, allowEmptyString) {
        //处理json为{}
        if (typeof value === "object") {
            for (let t in value) {
                return false
            }

            return true
        }

        return (value == "null") || (value == "undefined") || (value === null) || (value === undefined) || (!allowEmptyString ? value === '' : false) || ((toString.call(value) === '[object Array]') && value.length === 0);
    },

    /**
     * 格式化手机号，隐藏中间4位
     */
    formatMobile: function(mobile) {
        if (!this.isEmpty(mobile)) {
            return mobile.substring(0,3) + "****" + mobile.substring(8,11);
        }
        
        return "";
    },

    getQueryString: function(name, search=window.location.search) {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    },

    httpGetParam: function (name) {
        var url = window.location.href;
        var start = url.indexOf("?") + 1;
        if (start === 0) {
            return "";
        }
        var value = "";
        var queryString = url.substring(start);
        var paraNames = queryString.split("&");
        for (var i = 0; i < paraNames.length; i++) {
            var str = paraNames[i];
            start = str.indexOf("=");
            var parameterName = str;
            if (start !== -1) {
                parameterName = str.substring(0, start);
            }
            if (name === parameterName) {
                start = str.indexOf("=");
                if (start !== -1) {
                    value = str.substring(start + 1);
                    var end = str.indexOf("#");
                    if (end !== -1) {
                        value = str.substring(0, end);
                    }
                }
            }
        }
        return decodeURIComponent(value);
    },

    formatProjectStatus: function(row) {
        let x = row.status
        if (typeof row == 'string') {
            x = row
        }
        switch (x) {
            case 'working':
                return '进行中'
            case 'publish':
                return '待分配'
            case 'pending':
                return `投标中`
            case 'done':
                return '已完成'
            case 'reject':
                return '已拒绝'
            case 'draft':
                return '草稿'
            case 'cancel':
                return '已取消'
            case 'drop':
                return '已被抢'
            default:
                return status			
        }
    },
    getLastTime: function(expect_at) {
        let d = moment(expect_at).diff(moment(), 'days'),
            h = moment(expect_at).diff(moment(), 'hours')%24
        let overtime = d < 0
        return {overtime, d, h}
    },
    reslove_detail_url: function(project, user) {
        if (project.user_id == user.id || user.is_admin) {
             //我发布的
            return `/project-detail/introduction/publish?type=publish&id=${project.id}&project_id=${project.id}`
        }

        if (project.user_id !== user.id && project.self_Project_user==null) {
            //没关系
            return `/public-detail?project_id=${project.id}`
        }

        if (project.self_Project_user.type=='pending') {
            //我抢单的
            return `/project-detail/introduction/grab?type=grab&id=${project.self_Project_user.id}&project_id=${project.id}`
            
        } else {
            //我执行的
            return `/project-detail/introduction/execute?type=execute&id=${project.self_Project_user.id}&project_id=${project.id}`
        }
    },
    formatResource: (resource) => {
        let url = '//ceoja-1251886366.cos.ap-guangzhou.myqcloud.com/1519283316172file-placeholder.png',
          arr = []
        arr = resource.map( x => {
          if (/.+\.(png|jpg|gif|jpeg)$/.test(x.file)) {
            url = x.file
          } 
          return {url: url, uri: x.file}
        }) 
        return arr
      }

}

export default utils